﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Text;
using System.Collections;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using TS_XY_Train_Repaire.Locomotive.TrainRepair;

namespace TS_XY_Train_Repaire.UserCon.ucPub.Map
{
    public partial class uc_Pub_Map_Select_ListView_ByOne_CallBack : DevExpress.XtraEditors.XtraForm
    {
        public string _LoadType = "0";
        public string _LoadFormType = "0";
        public uc_Pub_Map_Select_ListView_ByOne_CallBack()
        {
            InitializeComponent();
        }

        private void uc_Pub_Map_Select_ListView_ByOne_CallBack_Load(object sender, EventArgs e)
        {
            TS_Win_Class.MySqlCom("update TS_S_B_Station_WorkPlace set f_stacknum=(select count(*) from TS_TR_BU_Work_Main where f_tpmapid=TS_S_B_Station_WorkPlace.f_id and f_state in (select f_id from TS_TR_B_Work_Main_State where f_del=0 and f_state<>9) and f_del=0) where f_del=0 and f_type=" + _LoadType);
            ArrayList pList = new ArrayList();
            LeverString_SysTreeMainList p = new LeverString_SysTreeMainList();
            string _str = "";
            string strSQL = "select f_type,f_id,f_name,f_stack-f_stacknum as f_stack,f_name+'  '+isnull((select top 1 f_ttname+f_name from TS_TR_BU_Work_Main where f_tpmapid=TS_S_B_Station_WorkPlace.f_id and f_state in (select f_id from TS_TR_B_Work_Main_State where f_del=0 and f_state<>9) and f_del=0),'')as f_trainname,f_level,f_stacknum from TS_S_B_Station_WorkPlace where f_del=0 and f_type in (3," + _LoadType + ")";
            SqlConnection conn = new SqlConnection(TS_Win_Class.GetSQlConnStr());
            conn.Open();
            SqlCommand comm = new SqlCommand(strSQL, conn);
            SqlDataReader read = comm.ExecuteReader();
            while (read.Read())
            {
                p = new LeverString_SysTreeMainList();

                if (read["f_level"].ToString() == ",")
                {
                }
                else
                {
                    _str = read["f_level"].ToString();
                    _str = _str.Substring(0, (_str).Length-1);
                    _str = _str.Substring(_str.LastIndexOf(',') + 1);
                    p.f_parent = _str;
                }
                p.f_name = read["f_name"].ToString();
                p.f_name_1 = read["f_trainname"].ToString();
                p.f_id = read["f_id"].ToString();
                p.f_num = (int)read["f_stacknum"];
                p.f_stack = (int)read["f_stack"];
                p.f_type = read["f_type"].ToString();
                pList.Add(p);
            }
            read.Close();
            conn.Close();
            conn.Dispose();
            tl_Level.DataSource = pList;
            tl_Level.ExpandAll();
        }

        private void bt_Sure_Click(object sender, EventArgs e)
        {
            try
            {
                //if (_LoadType == "0")
                //{
                //    if (_LoadFormType == "0")
                //    {
                //        Locomotive_Repair_Work_TrainIn_MainForm._MapName = tl_Level.FocusedNode.GetValue("f_name").ToString();
                //        Locomotive_Repair_Work_TrainIn_MainForm._MapID = tl_Level.FocusedNode.GetValue("f_id").ToString();
                //    }
                //    if (_LoadFormType == "1")
                //    {
                //        Locomotive_Repair_Work_Start_MainForm._MapName = tl_Level.FocusedNode.GetValue("f_name").ToString();
                //        Locomotive_Repair_Work_Start_MainForm._MapID = tl_Level.FocusedNode.GetValue("f_id").ToString();
                //    }
                //}
                //if (_LoadType == "1")
                //{
                //    UserCon.Locomotive.Fittings.Repair.uc_Locomotive_Fittings_Repair_FittIn_PopoForm._MapName = tl_Level.FocusedNode.GetValue("f_name").ToString();
                //    UserCon.Locomotive.Fittings.Repair.uc_Locomotive_Fittings_Repair_FittIn_PopoForm._MapID = tl_Level.FocusedNode.GetValue("f_id").ToString();
                //}
                //if (_LoadType == "2")
                //{
                //    UserCon.Equipment.Repair.uc_Equipment_Repair_EqupIn_PopoForm._MapName = tl_Level.FocusedNode.GetValue("f_name").ToString();
                //    UserCon.Equipment.Repair.uc_Equipment_Repair_EqupIn_PopoForm._MapID = tl_Level.FocusedNode.GetValue("f_id").ToString();
                //}
                if (tl_Level.FocusedNode.GetValue("f_type").ToString() == _LoadType)
                {
                    TextBox _tb = (TextBox)this.Tag;
                    _tb.Text = tl_Level.FocusedNode.GetValue("f_name").ToString();
                    _tb.Tag = tl_Level.FocusedNode.GetValue("f_id").ToString();
                    this.DialogResult = DialogResult.OK;
                }
                else
                {
                    MessageBox.Show("你选择的不是有效工位！");
                }
            }
            catch
            {
            }
        }

        private void bt_Exit_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }
    }
}